package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.domain.DataMaterialBatchHistory;
import com.uwlaser.service.DataMaterialBatchHistoryService;
import com.uwlaser.utils.BeanCopyUtil;
import com.uwlaser.utils.FileUtil;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.DataMaterialBatch;
import com.uwlaser.service.DataMaterialBatchService;
import com.uwlaser.service.dto.DataMaterialBatchDto;
import com.uwlaser.service.dto.DataMaterialBatchQueryParam;
import com.uwlaser.service.mapper.DataMaterialBatchMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DataMaterialBatchService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DataMaterialBatchServiceImpl extends CommonServiceImpl<DataMaterialBatchMapper, DataMaterialBatch> implements DataMaterialBatchService {

    // private final RedisUtils redisUtils;
    private final DataMaterialBatchMapper dataMaterialBatchMapper;

    private final DataMaterialBatchHistoryService dataMaterialBatchHistoryService;


    @Override
    public PageInfo<DataMaterialBatchDto> queryAll(DataMaterialBatchQueryParam query, Pageable pageable) {
        IPage<DataMaterialBatch> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DataMaterialBatch> page = dataMaterialBatchMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DataMaterialBatchDto.class);
    }

    @Override
    public List<DataMaterialBatchDto> queryAll(DataMaterialBatchQueryParam query){
        return ConvertUtil.convertList(dataMaterialBatchMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DataMaterialBatchDto.class);
    }

    @Override
    public DataMaterialBatch getById(Integer id) {
        return dataMaterialBatchMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DataMaterialBatchDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), DataMaterialBatchDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DataMaterialBatchDto resources) {
        DataMaterialBatch entity = ConvertUtil.convert(resources, DataMaterialBatch.class);
        return dataMaterialBatchMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DataMaterialBatchDto resources){
        DataMaterialBatch entity = ConvertUtil.convert(resources, DataMaterialBatch.class);
        int ret = dataMaterialBatchMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // 通过id查出需要解绑的单件物料
        List<DataMaterialBatch> dataMaterialSingleList = dataMaterialBatchMapper.selectBatchIds(ids);
        List<DataMaterialBatchHistory> list = BeanCopyUtil.copyListToList(dataMaterialSingleList, DataMaterialBatchHistory.class);
        //新增物料数据（历史数据记录）
        dataMaterialBatchHistoryService.saveBatch(list);
        return dataMaterialBatchMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    @Override
    public void download(List<DataMaterialBatchDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DataMaterialBatchDto dataMaterialBatch : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("产品SN", dataMaterialBatch.getProductSn());
              map.put("产品型号", dataMaterialBatch.getProductCode());
              map.put("产品类型", dataMaterialBatch.getProductType());
              map.put("订单编号", dataMaterialBatch.getOrderCode());
              map.put("工单编号", dataMaterialBatch.getWorkOrderCode());
              map.put("产线编号", dataMaterialBatch.getLienCode());
              map.put("工段编号", dataMaterialBatch.getSectionCode());
              map.put("工位编号", dataMaterialBatch.getStationCode());
              map.put("工序编号", dataMaterialBatch.getOperationCode());
              map.put("设备编号", dataMaterialBatch.getDeviceCode());
              map.put("物料编号", dataMaterialBatch.getMaterialSn());
              map.put("物料名称", dataMaterialBatch.getMaterialName());
              map.put("物料编码", dataMaterialBatch.getMaterialCode());
              map.put("使用数量", dataMaterialBatch.getQuantity());
              map.put("物料单位", dataMaterialBatch.getMaterialUnit());
              map.put("过站ID", dataMaterialBatch.getPassStationId());
              map.put("创建人", dataMaterialBatch.getCreateBy());
              map.put("创建时间", dataMaterialBatch.getCreateTime());
              map.put("更新人", dataMaterialBatch.getUpdateBy());
              map.put("更新时间", dataMaterialBatch.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }
}
